package com.haochang.base;

import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.user.UserToken;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.VodActivity;
import com.haochang.chunkvod.controller.activity.login.LoginActivity;
import com.haochang.chunkvod.controller.activity.login.PasswordModifyActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * Created by lxl on 16/5/10.
 */
public abstract class BaseFragmentActivity extends FragmentActivity
{

	private boolean isInit = false;
	public Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.context = this;
		//		BaseActivityManager.getInstance().addActivity(this);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		if (!isInit)
		{
			isInit = true;
			receiveParam();
			initData();
			initViews();
			initViewsData();
		}
	}

	/**
	 * 回到首页
	 */
	public void backToHome()
	{
		Intent intent = new Intent(context, VodActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

	/**
	 * 修改密码
	 */
	public void goToPasswordModify()
	{
		startActivity(new Intent(context, PasswordModifyActivity.class));
	}

	/**
	 * 退出登录
	 */
	public void loginOut()
	{
		new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure).cancelable(false)
				.contentName("确定要退出登录吗？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
				{
					@Override
					public void onOnlyCancelClick()
					{

					}

					@Override
					public void onOkClick()
					{
						Intent intent1 = new Intent(context, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent1);
						//退出登录，关闭之前的所有activity
						//BaseActivityManager.getInstance().closeAllActivity();
						UserToken.reset();
					}

					@Override
					public void onCancelClick()
					{

					}
				}).build().show();
	}

	/**
	 * receive param form otherActivity
	 */
	public abstract void receiveParam();

	/**
	 * init current activity need data
	 */
	public abstract void initData();

	/**
	 * init all views
	 */
	public abstract void initViews();

	/**
	 * load view data
	 */
	public void initViewsData()
	{

	}

	@Override
	public void onResume()
	{
		super.onResume();
		BaseActivityManager.getInstance().onResume(this);
	}

	@Override
	public void onPause()
	{
		super.onPause();
		BaseActivityManager.getInstance().onPause(this);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		isInit = false;
	}
}
